package yag.battle.pet.dao;

import com.mysql.jdbc.Statement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import yag.battle.pet.model.Pet;
import yag.battle.pet.model.PetMapper;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by user on 31.08.2014.
 */
@Repository
public class PetDao {

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }

    public int create(Pet pet) {
        KeyHolder holder = new GeneratedKeyHolder();
        final String sql = "insert into pets (mood) values (?) ";
        final Pet innerPet = pet;
        jdbcTemplateObject.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection)
                    throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setInt(1, innerPet.getMood());
                return ps;
            }
        }, holder);
        int id = holder.getKey().intValue();
        return id;
    }

    public Pet getById(Integer id) {
        String SQL = "select * from pets where id = ?";
        Pet pet = jdbcTemplateObject.queryForObject(SQL,
                new Object[]{id}, new PetMapper());
        return pet;
    }

    public Pet getByUser(Integer id) {
        String SQL = "select * from pets where id_user = ?";
        Pet pet = jdbcTemplateObject.queryForObject(SQL,
                new Object[]{id}, new PetMapper());
        return pet;
    }

    public List<Pet> getList() {
        String SQL = "select * from pets";
        List<Pet> pets = jdbcTemplateObject.query(SQL,
                new PetMapper());
        return pets;
    }

    public void updateUserId(int id, int idUser) {
        String SQL = "update pets set id_user = ? where id = ?";
        jdbcTemplateObject.update(SQL, idUser, id);
        return;
    }

    public void updateById(Integer id, Pet pet) {
        String SQL = "update pets set mood = ? where id = ?";
        jdbcTemplateObject.update(SQL, pet.getMood());
        return;
    }

    public void updateByUserId(int idUser, Pet pet) {
        String SQL = "update pets set  mood = ? where id_user = ?";
        jdbcTemplateObject.update(SQL, pet.getMood(), idUser);
        return;
    }

    public void deleteById(Integer id) {
        String SQL = "delete from pets where id = ?";
        jdbcTemplateObject.update(SQL, id);
        return;
    }

    public void deleteByUserId(Integer id) {
        String SQL = "delete from pets where id_user = ?";
        jdbcTemplateObject.update(SQL, id);
        return;
    }

    public void deleteUserIdById(int id) {
        String SQL = "update pets set id_user = null where id = ?";
        jdbcTemplateObject.update(SQL, id);
        return;
    }

}
